﻿using System;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Boxie
{
    public partial class frmConfig : Form
    {
        public TextBox txtIP;

        public frmConfig()
        {
            InitializeComponent();
            txtIP = txtInternet_UniqueID;
            if (BoxieRegLib.GetSettingBool("General/WindowTopMost", true))
                this.TopMost = true;
        }

        private void frmConfig_Load(object sender, EventArgs e)
        {
            // General
            ckGeneral_AutoClose.Checked = BoxieRegLib.GetSettingBool("General/AutoClose", false);
            ckGeneral_LogVisible.Checked = BoxieRegLib.GetSettingBool("General/LogVisible", false);
            ckGeneral_SmartTyping.Checked = BoxieRegLib.GetSettingBool("General/SmartTyping", true);
            ckGeneral_Snapping.Checked = BoxieRegLib.GetSettingBool("General/SnapToEdge", true);
            txtGeneral_SnapDistance.Text = BoxieRegLib.GetSettingString("General/SnapDistance", "100");
            ckGeneral_CheckUpdates.Checked = BoxieRegLib.GetSettingBool("General/CheckUpdates", true);
            ckGeneral_TopMost.Checked = BoxieRegLib.GetSettingBool("General/WindowTopMost", true);
            // FTP
            txtFTP_Hostname.Text = BoxieRegLib.GetSettingString("FTP/Hostname", "ftp.example.com");
            txtFTP_Port.Text = BoxieRegLib.GetSettingString("FTP/Port", "21");
            txtFTP_Username.Text = BoxieRegLib.GetSettingString("FTP/Username", "");
            txtFTP_Password.Text = BoxieRegLib.GetSettingString("FTP/Password", "");
            txtFTP_FtpLoc.Text = BoxieRegLib.GetSettingString("FTP/FtpLocation", "/some/dir/");
            txtFTP_WebLoc.Text = BoxieRegLib.GetSettingString("FTP/WebLocation", "http://www.example.com/some/dir/");
            ckFTP_Clipboard.Checked = BoxieRegLib.GetSettingBool("FTP/Clipboard", true);
            // Internet
            txtInternet_SaveFile.Text = BoxieRegLib.GetSettingString("Internet/SaveFile", Application.StartupPath);
            LoadAliases();
            // Drawing
            txtDrawing_UpdateInterval.Text = BoxieRegLib.GetSettingString("Drawing/UpdateInterval", "1000");
            // Auto
            ckAuto_MusicAuto.Checked = BoxieRegLib.GetSettingBool("Auto/AutoMusicTag", false);
            txtAuto_MusicFormat.Text = BoxieRegLib.GetSettingString("Auto/MusicFormat", "%13% - %21%.%ext%");
            ckAuto_MusicMove.Checked = BoxieRegLib.GetSettingBool("Auto/AutoMove", false);
            txtAuto_MusicMove.Text = BoxieRegLib.GetSettingString("Auto/MoveLocation", "");

            // Logic - General
            if (ckGeneral_Snapping.Checked)
                txtGeneral_SnapDistance.Enabled = true;
            else
                txtGeneral_SnapDistance.Enabled = false;
        }

        private void ckGeneral_AutoClose_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("General/AutoClose", ckGeneral_AutoClose.Checked);
        }

        private void txtFTP_Hostname_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("FTP/Hostname", txtFTP_Hostname.Text);
        }

        private void txtFTP_Port_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("FTP/Port", txtFTP_Port.Text);
        }

        private void txtFTP_Username_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("FTP/Username", txtFTP_Username.Text);
        }

        private void txtFTP_Password_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("FTP/Password", txtFTP_Password.Text);
        }

        private void txtFTP_FtpLoc_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("FTP/FtpLocation", txtFTP_FtpLoc.Text);
        }

        private void txtFTP_WebLoc_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("FTP/WebLocation", txtFTP_WebLoc.Text);
        }

        private void ckFTP_Clipboard_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("FTP/Clipboard", ckFTP_Clipboard.Checked);
        }

        private void ckGeneral_LogVisible_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("General/LogVisible", ckGeneral_LogVisible.Checked);
        }

        private void tabPage3_Enter(object sender, EventArgs e)
        {
            // generate unique connection id
            if ((txtInternet_UniqueID.Text == "")||(txtInternet_UniqueID.Text == "Aquiring IP Address..."))
            {
                txtInternet_UniqueID.Text = "Aquiring IP Address...";
                BoxieThreadLib.GetIP(this);
            }
        }

        private void txtInternet_SaveFile_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("Internet/SaveFile", txtInternet_SaveFile.Text);
        }

        private void btnBrowseSaveFileLoc_Click(object sender, EventArgs e)
        {
            folderBrowser.ShowDialog();
            if(folderBrowser.SelectedPath!="")
                txtInternet_SaveFile.Text = folderBrowser.SelectedPath;
        }

        private void btnAliasAdd_Click(object sender, EventArgs e)
        {
            frmConfigAddAlias f = new frmConfigAddAlias(lstInternet_Aliases);
            f.ShowDialog();
            SaveAliases();
        }

        public void SaveAliases()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lstInternet_Aliases.Items.Count; i++)
            {
                sb.Append(lstInternet_Aliases.Items[i].Text + ":" + lstInternet_Aliases.Items[i].SubItems[1].Text + "/");
            }
            BoxieRegLib.SaveSettingString("Internet/Aliases", sb.ToString());
        }

        public void LoadAliases()
        {
            lstInternet_Aliases.Items.Clear();
            string items = BoxieRegLib.GetSettingString("Internet/Aliases", "");
            string[] itemArray = items.Split('/');
            for (int i = 0; i < itemArray.GetUpperBound(0); i++)
            {
                ListViewItem lvi = lstInternet_Aliases.Items.Add(itemArray[i].Split(':')[0]);
                lvi.SubItems.Add(itemArray[i].Split(':')[1]);
            }
        }

        private void btnAliasRemove_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lstInternet_Aliases.SelectedItems[0].Index;
                lstInternet_Aliases.Items.RemoveAt(index);
                SaveAliases();
            }
            catch
            {
            }
        }

        private void ckGeneral_SmartTyping_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("General/SmartTyping", ckGeneral_SmartTyping.Checked);
            frmBoxie.instance.enableSmartTyping = ckGeneral_SmartTyping.Checked;
        }

        private void ckGeneral_Snapping_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("General/SnapToEdge", ckGeneral_Snapping.Checked);
            if (ckGeneral_Snapping.Checked)
                txtGeneral_SnapDistance.Enabled = true;
            else
                txtGeneral_SnapDistance.Enabled = false;
        }

        private void txtGeneral_SnapDistance_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("General/SnapDistance", txtGeneral_SnapDistance.Text);
            try
            {
                frmBoxie.instance.SnapDist = Convert.ToInt32(txtGeneral_SnapDistance.Text);
            }
            catch
            {
                frmBoxie.instance.SnapDist = 100;
            }
        }

        private void ckGeneral_CheckUpdates_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("General/CheckUpdates", ckGeneral_CheckUpdates.Checked);
        }

        private void ckGeneral_TopMost_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("General/WindowTopMost", ckGeneral_TopMost.Checked);
        }

        private void txtDrawing_UpdateInterval_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("Drawing/UpdateInterval", txtDrawing_UpdateInterval.Text);
        }

        private void ckAuto_MusicAuto_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("Auto/AutoMusicTag", ckAuto_MusicAuto.Checked);
        }

        private void txtAuto_MusicFormat_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("Auto/MusicFormat", txtAuto_MusicFormat.Text);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.ShowDialog();
            txtAuto_MusicMove.Text = folderBrowserDialog.SelectedPath;
        }

        private void txtAuto_MusicMove_TextChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingString("Auto/MoveLocation", txtAuto_MusicMove.Text);
        }

        private void ckAuto_MusicMove_CheckedChanged(object sender, EventArgs e)
        {
            BoxieRegLib.SaveSettingBool("Auto/AutoMove", ckAuto_MusicMove.Checked);
        }
    }
}
